package com.flaremars.picker.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;

import com.flaremars.picker.common.DrawingObject;


/**
 * Created by FlareMars on 2016/3/4.
 */
public class PathDrawing implements DrawingObject {

    private Path path;
    private float size;

    private float mX;
    private float mY;

    public PathDrawing(float size) {
        this.size = size;
        path = new Path();
    }

    public Path getPath() {
        return path;
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        float originSize = paint.getStrokeWidth();
        paint.setStrokeWidth(size);
        canvas.drawPath(path, paint);
        paint.setStrokeWidth(originSize);
    }

    @Override
    public void handleTouchDown(MotionEvent event) {
        path.moveTo(event.getX(), event.getY());
        mX=event.getX();
        mY=event.getY();
    }

    @Override
    public void handleTouchMove(MotionEvent event) {

        float dx = Math.abs(event.getX() - mX);
        float dy = Math.abs(event.getY() - mY);
        if (dx >= 4 || dy >= 4) {
            path.quadTo(mX, mY, (event.getX() + mX) / 2, (event.getY() + mY) / 2);
            mX = event.getX();
            mY = event.getY();
        }
    }

    @Override
    public void handleTouchUp(MotionEvent event) {
        path.lineTo(event.getX(), event.getY());
    }
}
